<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdmin;
use function Couchbase\defaultDecoder;
use GatewayWorker\Lib\Gateway;
use think\Db;
use think\Request;
use app\admin\model\Company as CompanyModel;

class Company extends BaseAdmin
{

    /**
     * 列表
     * @return mixed
     */
    public function index(Request $request){
        if (request()->isAjax()){
            return CompanyModel::getlist($request);
        }else{

            $pageno = input('param.pageno', 1);

            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }


    /**
    **修改添加
    **/
    public function set(){
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();

                if(!$post['nickName']){
                    return ['status'=>false,'message'=>'姓名不能为空'];
                }
                
                if(!empty($post['avatarUrl'])){
                    $post['avatarUrl'] = $this->base64_upload($post['avatarUrl'], 'user');
                }

                $id = !empty($post['id'])?$post['id']:0;
                if($id){
                    unset($post['id']);
                    $res = Db::name('user')->where('id', $id)->update($post);
                    if($res!==false){
                        return ['status'=>true, 'message'=>'更新成功'];
                    }else{
                        return ['status'=>false,'message'=>'更新失败'];
                    }
                }else{
                    $res = Db::name('user')->insertGetId($post);
                    if($res){
                        return ['status'=>true, 'message'=>'新增成功'];
                    }else{
                        return ['status'=>false,'message'=>'新增失败'];
                    }
                }
            }else{
                $id = request()->get('id');
                $user = Db::name('user')->where('id', $id)->find();
                return $user;
            }
        }else{
            $pageno = request()->get('pageno')?request()->get('pageno'):1;
            $this->assign('id',request()->get('id'));
            $this->assign('pageno', $pageno);
            return $this->fetch();
        }
    }



    /**
     * 删除
     * @return array
     */
    public function remove(){
        $ids = request()->post('ids');
        if(request()->isAjax()){
            $ids = json_decode($ids, true);
            $result = Db::name('user')->delete($ids);
            if($result){
                return ['status'=>true,'message'=>'删除成功!'];
            }else{
                return ['status'=>false,'message'=>'删除失败!'];
            }
        }else{
            new \HttpException(502,'请求不合法');
        }
    }


    
    public function config(){
        if (request()->isAjax()){
            if(request()->isPost()){
                $post = request()->post();
                $imgArr = [];
                if(!empty($post['ad'])){
                    $post['ad'] = $post['ad']?join(',',$post['ad']):'';
                }
                $config = Db::name('config')->find();
                $post['basic_price'] && $post['basic_price'] = $post['basic_price']*100;
                $post['repair_10'] && $post['repair_10'] = $post['repair_10']*100;
                $post['repair_24'] && $post['repair_24'] = $post['repair_24']*100;

                if($config){
                    $res = db('config')->where('id', $config['id'])->update($post);
                }else{
                    $res = db('config')->insertGetId($post);
                }

                if($res !== false){
                    return ['status'=>true,'message'=>'编辑成功'];
                }else{
                    return ['status'=>false,'message'=>'编辑成功'];
                }
            }else{
                $config = Db::name('config')->find();
                if($config){
                    $config['basic_price'] = $config['basic_price']/100;
                    $config['repair_10'] = $config['repair_10']/100;
                    $config['repair_24'] = $config['repair_24']/100;
                    $ad = [];
                    if(!empty($config['ad'])){
                        $ad = explode(',', $config['ad']);
                    }
                    $config['ad'] = $ad;
                }
                return $config;
            }
        }else{
            return $this->fetch();
        }
    }

    public function getonline()
    {
        $data = Gateway::getAllClientIdList();
        dump($data);
    }

    # 审核通过
    public function pass(Request $request)
    {
        $company = \app\admin\model\Company::find($request->post('id'));
        $company->status = 2;
        $company->save();
        $user = \app\admin\model\User::find($company->user_id);
       if ($user) {
           $user->status = 3;
           $user->save();
           return returnData();
       }
        return returnData([],'用户身份修改失败,此用户不存在');

    }

    

    

}